package org.jeecg.modules.wechat.service.Impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.wechat.api.WxMiniApi;
import org.jeecg.constant.WXConstant;
import org.jeecg.modules.wechat.util.WeChatUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * TODO: 实现类
 *
 * @author Zhang Hao yu
 * @date 2020/11/10 17:25
 */
@Service
public class WxMinApiImpl implements WxMiniApi {

    @Override
    public JSONObject authCode2Session(String appid, String secret, String code) {
        //微信接口服务,通过调用微信接口服务中jscode2session接口获取到openid和session_key
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WXConstant.WX_APPID + "&secret=" + WXConstant.WX_SECRET + "&js_code=" + code + "&grant_type=authorization_code";
        String str = WeChatUtil.httpRequest(url, "GET", null);
        if (StringUtils.isEmpty(str)){
            return null;
        }
        return JSONObject.parseObject(str);
    }
}